package com.ruoyi.web.controller.user;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.framework.web.service.AppLoginService;
import com.ruoyi.server.domain.ServerRegion;
import com.ruoyi.server.service.IServerRegionService;
import com.ruoyi.user.service.IAppUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Validated
@Api(value = "APP用户中心", tags = "APP用户中心接口")
@RestController
@RequestMapping("/app/user")
public class AppUserController extends BaseController {

    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IServerRegionService serverRegionService;
    @Resource
    private AppLoginService appLoginService;

    /**
     * 区服列表
     */
    @ApiOperation(value = "区服列表")
    @GetMapping("/regionList")
    public AjaxResult regionList() {
        List<ServerRegion> list = serverRegionService.selectList();
        return success(list);
    }

    @ApiOperation("切换区服")
    @GetMapping("/switchoverRegionId")
    public AjaxResult switchoverRegionId(@ApiParam(value = "区服id", required = false)
                                         @RequestParam(required = false) Long regionId) {
        AppUser appUser = appUserService.switchoverRegionId(getLoginAppUser().getAppUser(), regionId);
        appLoginService.setLoginAppUser(getLoginAppUser(), appUser);
        return toAjax(Boolean.TRUE);
    }

    @ApiOperation("绑定QQ")
    @GetMapping("/bindQQ")
    public AjaxResult bindQQ(@ApiParam(value = "QQ号码", required = false)
                             @RequestParam(required = false) String qqCode) {
        AppUser appUser = appUserService.bindQQ(getLoginAppUser().getAppUser(), qqCode);
        appLoginService.setLoginAppUser(getLoginAppUser(), appUser);
        return toAjax(Boolean.TRUE);
    }

    @ApiOperation("绑定手机号")
    @GetMapping("/bindPhoneNumber")
    public AjaxResult getPhoneNumber(@ApiParam(value = "phoneNumber") @Valid @NotBlank(message = "phoneNumber不能为空") String phoneNumber) {
        AppUser appUser = appUserService.bindPhoneNumber(getLoginAppUser(), phoneNumber);
        appLoginService.setLoginAppUser(getLoginAppUser(), appUser);
        return success();
    }

}
